<?php
/**
 * 报价单详情Service
 */

declare (strict_types = 1);

namespace app\api\service;

use app\api\exception\ApiServiceException;
use app\common\model\Brand;
use app\common\model\Cate;
use app\common\model\OfferDetail;
use app\common\model\Spec;
use app\common\model\Type;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

class OfferDetailService extends ApiBaseService {
	protected OfferDetail $model;

	public function __construct() {
		$this->model = new OfferDetail();
	}

	/**
	 * 列表
	 * @param $param
	 * @param $page
	 * @param $limit
	 * @return array
	 * @throws ApiServiceException
	 */
	public function getList($param, $page, $limit): array {
		try {
			$field = 'id,offer_id,school_id,name,money,status,create_time';
			$data = $this->model
				->scope('ApiWhere', $param)
				->page($page, $limit)
				->field($field)
				->select()
				->toArray();
		} catch (DataNotFoundException | ModelNotFoundException $e) {
			$data = [];
		} catch (DbException $e) {
			throw new ApiServiceException('查询列表失败，信息' . $e->getMessage());
		}

		return $data;
	}

	/**
	 * 添加
	 * @param $param
	 * @return bool
	 */
	public function createData($param): bool {
		$result = $this->model::create($param);
		return (bool) $result;
	}

	/**
	 * 数据详情
	 * @param $id
	 * @return array
	 * @throws ApiServiceException
	 */
	public function getDataInfo($id): array {
		$data = $this->model->with(['school'])->where('id', '=', $id)->findOrEmpty();
		if ($data->isEmpty()) {
			throw new ApiServiceException('数据不存在');
		}
		// $list = $this->model->where('school_id', $data['school_id'])->where('send_time', strtotime($data['send_time']))->select()->toArray();
		// $data['money'] = array_sum(array_column($list, 'money'));
		// $data['detail'] = array_column($list, 'detail');
		// $result = [];

		// // 遍历原始三维数组
		// foreach ($data['detail'] as $a) {
		// 	foreach ($a as $b) {
		// 		//将第三层的数组添加到二维数组中
		// 		$result[] = $b;
		// 	}
		// }
		// $data['detail'] = $result;

		$detail = $data['detail'];
		foreach ($data['detail'] as $key => $value) {
			$detail[$key]['type'] = Type::where('id', $value['type_id'])->find();
			$detail[$key]['type']['cate'] = Cate::where('id', $detail[$key]['type']['cate_id'])->find();
			//$detail[$key]['menu'] = Menu::where('id', $value['menu_id'])->find();

			$detail[$key]['brand'] = Brand::where('id', $value['brand_id'])->find();

			$detail[$key]['spec'] = Spec::where('id', $value['spec_id'])->find();

		}
		$data['detail'] = $detail;
		return $data->toArray();
	}

	/**
	 * 修改
	 * @param $id
	 * @param $param
	 * @return bool
	 * @throws ApiServiceException
	 */
	public function updateData($id, $param): bool {
		$data = $this->model->where('id', '=', $id)->findOrEmpty();
		if ($data->isEmpty()) {
			throw new ApiServiceException('数据不存在');
		}
		$result = $data->save($param);

		if (!$result) {
			throw new ApiServiceException('更新失败');
		}

		return true;
	}

	/**
	 * 删除
	 * @param mixed $id
	 * @return bool
	 * @throws ApiServiceException
	 */
	public function deleteData($id): bool {
		$result = $this->model::destroy(function ($query) use ($id) {
			$query->whereIn('id', $id);
		});

		if (!$result) {
			throw new ApiServiceException('更新失败');
		}

		return true;
	}

}
